package com.ogilvy.audi.webapp.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.ogilvy.audi.model.CircleAlbum;
import com.ogilvy.audi.model.CircleCategory;
import com.ogilvy.audi.model.CircleTopic;
import com.ogilvy.audi.service.CircleCategoryManager;
import com.ogilvy.audi.service.GenericManager;
import com.ogilvy.audi.util.ServiceDispatcher;
import com.ogilvy.audi.webapp.action.page.PageAction;
import com.opensymphony.xwork2.Preparable;

public class CircleCategoryAction extends PageAction implements Preparable {
    private CircleCategoryManager circleCategoryManager;
    private List circleCategories;
    private CircleCategory circleCategory;
    private CircleCategory circle;
    private CircleAlbum circleAlbum;
    private CircleTopic circleTopic;
    private Long circleId;
    private String query;
    public String id;
	public String parentId;
	
	public List<CircleCategory> cirList;
	public List<CircleCategory> hostList;
	public List<?> topList;
	public List<CircleTopic> topListSimple;
	public List<CircleTopic> hotCirList;
	public List<CircleAlbum> albumList;
	public String msg;
	//@Resource
	public ServiceDispatcher serviceDispatcher;
	

    public ServiceDispatcher getServiceDispatcher() {
		return serviceDispatcher;
	}

	public void setServiceDispatcher(ServiceDispatcher serviceDispatcher) {
		this.serviceDispatcher = serviceDispatcher;
	}

	public void setCircleCategoryManager(CircleCategoryManager circleCategoryManager) {
        this.circleCategoryManager = circleCategoryManager;
    }

    public List getCircleCategories() {
        return circleCategories;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String circleCategoryId = getRequest().getParameter("circleCategory.circleId");
            if (circleCategoryId != null && !circleCategoryId.equals("")) {
                circleCategory = circleCategoryManager.get(new Long(circleCategoryId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
         searchCondition(null);
      //  circleCategories = circleCategoryManager.search(query, CircleCategory.class);
        return SUCCESS;
    }

    public void setCircleId(Long circleId) {
        this.circleId = circleId;
    }

    public CircleCategory getCircleCategory() {
        return circleCategory;
    }

    public void setCircleCategory(CircleCategory circleCategory) {
        this.circleCategory = circleCategory;
    }

    public String delete() {
        circleCategoryManager.remove(circleCategory.getCircleId());
        saveMessage(getText("circleCategory.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (circleId != null) {
            circleCategory = circleCategoryManager.get(circleId);
        } else {
            circleCategory = new CircleCategory();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (circleCategory.getCircleId() == null);

        circleCategoryManager.save(circleCategory);

        String key = (isNew) ? "circleCategory.added" : "circleCategory.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

    @Override
    public GenericManager getManager() {
        return circleCategoryManager;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public String getTableId() {
        return "circleCategories";  //To change body of implemented methods use File | Settings | File Templates.
    }
    
    //上面代码是由脚手架生成
 // 查询一级圈子，在首页显示
 	public String queryMainCircles() {
 		Map<String, Object> condition = new HashMap<String, Object>();
 		condition.put("parentId", "-1");
 		System.out.println("__________gg______serviceDispatcher_gg___________"+serviceDispatcher);
 		cirList = serviceDispatcher.getCircleCategoryManager().list(condition);
 		hostList = serviceDispatcher.getCircleCategoryManager().listByHot();
 		topListSimple = serviceDispatcher.getCircleTopicManager().getRecordsByHql("from CircleTopic where circleId = 1", null);
 		System.out.println("跳转页面__________________________");
 		return "cindex";
 	}
 // 查询二级圈子列表页
 	public String queryChildCircles(){
 		cirList = serviceDispatcher.getCircleCategoryManager().getRecordsByHql("from CircleTopic where parentId = " +parentId,null);
 		return "childList";
 	}
 	//查询圈子详细页（某圈子的首页）
 	public String queryDetailCircles(){
 		cirList = serviceDispatcher.getCircleCategoryManager().getRecordsByHql("from CircleTopic where parentId = " +parentId,null);
 		circle = serviceDispatcher.getCircleCategoryManager().get(Long.parseLong(parentId));
 		topListSimple = serviceDispatcher.getCircleTopicManager().getRecordsByHql("from CircleTopic where parentId = " +parentId,null);
 		albumList = serviceDispatcher.getCircleAlbumManager().getRecordsByHql("from CircleAlbum where parentId = "+parentId,null);
 		return "detail";
 	}
}